/**
 * 回访详情  初始化
 */
var ApprovalProcess = {
    id: "ApprovalProcessTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

var myDate = new Date();
var month;
var day;
if ((myDate.getMonth() + 1) < 10) {
    month = "0" + (myDate.getMonth() + 1);
} else {
    month = "" + (myDate.getMonth() + 1);
}
if (myDate.getDate() < 10) {
    day = "0" + myDate.getDate();
} else {
    day = "" + myDate.getDate();
}

/**
 * 初始化表格的列
 */
ApprovalProcess.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: '', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '', field: 'reservationsId', visible: false, align: 'center', valign: 'middle'},
        {title: '客户姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true},
        {title: '手机号码', field: 'customerPhone', align: 'center', valign: 'middle', sortable: true},
        {title: '门店', field: 'outpatientName', align: 'center', valign: 'middle', sortable: true},
        {title: '原建档人', field: 'name', align: 'center', valign: 'middle', sortable: true},
        {title: '新建档人', field: 'user', align: 'center', valign: 'middle', sortable: true},
        {title: '原一级渠道', field: 'sOneChannel', align: 'center', valign: 'middle', sortable: true},
        {title: '原二级渠道', field: 'sTwoChannel', align: 'center', valign: 'middle', sortable: true},
        {title: '原三级渠道', field: 'sThrChannel', align: 'center', valign: 'middle', sortable: true},
        {title: '一级渠道', field: 'oneChannel', align: 'center', valign: 'middle', sortable: true},
        {title: '二级渠道', field: 'twoChannel', align: 'center', valign: 'middle', sortable: true},
        {title: '三级渠道', field: 'thrChannel', align: 'center', valign: 'middle', sortable: true},
        {title: '审核状态', field: 'status', align: 'center', valign: 'middle', sortable: true,
            formatter: function (value, row, index) {
                if (value == 0) {
                    return "待审核";
                } else if (value == 1) {
                    return "已审核"
                } else if (value == 2) {
                    return "分管主任审核"
                }
            }
        },
        {title: '审核结果', field: 'result', align: 'center', valign: 'middle', sortable: true,
            formatter: function (value, row, index) {
                if (value == 0) {
                    return "未同意";
                } else if (value == 1) {
                    return "已同意"
                }
            }
        },
        {title: '提交时间', field: 'operationTime', align: 'center', valign: 'middle', sortable: true},
        {title: '备注', field: 'remarks', align: 'center', valign: 'middle', sortable: true},
        {title: '操作', field: 'id', align: 'center', valign: 'middle', sortable: true,
            formatter: function (value, row, index) {
                var ids = row.id+","+row.reservationsId;
                return '<span onclick="ApprovalProcess.modify(' + "'" + ids+ "'" + ')" style="font-size: 20px;font-weight: bold;cursor: pointer">【修改渠道审核】</span>'
                // return '<button title="审核" onclick="ApprovalProcess.modify(' + "'" + ids+ "'" + ')" type="button" class="btn" style="background: #ffffff;border: 1px solid #cccccc;"><span class="glyphicon glyphicon-pencil"></span></button>'
            }
        }
    ]
    return columns;
};

/**
 * 查询 回访详情列表
 */
ApprovalProcess.search = function () {
    ApprovalProcess.table.refresh({query: ApprovalProcess.formParams()});
};

/**
 * 重置
 */
ApprovalProcess.resetSearch = function () {
    $("#outpatientName").val(""); //  门诊
    $("#startTime").val(""); //开始时间
    $("#endTime").val("");  //结束时间
    $("#status").val(""); //  门诊
    $("#result").val(""); //  门诊
    document.getElementById("startTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "00:00:00";
    document.getElementById("endTime").value =myDate.getFullYear() + "-" + month + "-" + day + " " + "23:59:59";
    ApprovalProcess.search();
};

ApprovalProcess.modify = function (ids) {
    var index = layer.open({
        type: 2,
        title: '修改渠道',
        area: ['60%', '60%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservations/reservations_modify/' + ids
    });
    this.layerIndex = index;
};

/**
 *  选择建档人
 */
ApprovalProcess.getArchiving = function () {
    var index = layer.open({
        type: 2,
        title: '选择建档人',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservation/to_archiving'
    });
    this.layerIndex = index;
};

/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
ApprovalProcess.formParams = function () {
    var queryData = {};
    queryData['outpatientName'] = $("#outpatientName").val();  //门诊
    queryData['status'] = $("#status").val();  //审核状态
    queryData['result'] = $("#result").val();  //审核结果
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['archiving'] = $("#archiving").val();
    return queryData;
}

$(function () {
    document.getElementById("startTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "00:00:00";
    document.getElementById("endTime").value =myDate.getFullYear() + "-" + month + "-" + day + " " + "23:59:59";
    //初始化下拉到店门诊列表
    $.post(Feng.ctxPath + "/outpatientManagement/getList", function (data) {
        var txt = '<option value="">--请选择到店门诊--</option>';
        console.log(data);
        for (var i = 0; i < data.length; i++) {
            txt += '<option value="' + data[i].simplename + '">' + data[i].simplename + '</option>';
        }
        document.getElementById('outpatientName').innerHTML = txt;
        //状态单选
        var seleteooutpatientName = $('#outpatientNameValue').val();
        if (seleteooutpatientName != "") {
            $("#outpatientName option[value=" + seleteooutpatientName + "]").attr("selected", "selected");
        }
    }, "json");

    var defaultColunms = ApprovalProcess.initColumn();
    var table = new BSTable(ApprovalProcess.id, "/approvalProcess/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(ApprovalProcess.formParams());
    ApprovalProcess.table = table.init();
    ApprovalProcess.search();

    //采用定时器监听 时间内容是否变化
    var oldStartValue = $("#startTime").val();
    var oldEndtimeValue = $("#endTime").val();

    var oldArchivingValue = $("#archiving").val();
    setInterval(
        function () {//定时执行方法内容
            if ($("#startTime").val() != oldStartValue || $("#endTime").val() != oldEndtimeValue) {//如果时间发生变化，就触发查询事件
                ApprovalProcess.search();
                //如果值发生变化之后，把最新的值赋值到老的变量里面
                oldStartValue = $("#startTime").val();
                oldEndtimeValue = $("#endTime").val();
            }
            if (oldArchivingValue != $("#archiving").val()) {
                ApprovalProcess.search();
                oldArchivingValue = $("#archiving").val();//每次判断为修改后把最新的文本框的值给原有变量，为下一次循坏执行方法体里面的判断做准备
            }
        }, 500);
});
